<?php
/**
 * SignMiddleware
 *
 * @Description 描述
 * @Author look
 * @CreateTime 2022/2/23
 */

namespace app\admin\middleware;

use app\common\utils\AuthSign;

/**
 * 签名校验中间件
 */
class SignMiddleware {

    private $headers;

    public function handle($request, \Closure $next)
    {
        $this->headers = $request->header();
        $time = $this->getValue('time');
        $notify = $this->getValue('notify');
        $sign = $this->getValue('sign');
        $version = $this->getValue('version');
        $platform = $this->getValue('platform');
        (new AuthSign($time, $notify, $sign, $version, $platform))->checkSign();
        return $next($request);
    }

    private function getValue(string $key, mixed $default = null) {
        return $this->headers[$key] ?? $default;
    }
}